<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of sinhvien
 *
 * @author Dang Trung
 */
class Sinhvien extends CI_Controller {

    public function __construct() {
        parent::__construct();
    }

    private $user = '';

    public function checkUser() {
        $this->user = $this->session->userdata['user'];
    }

    public function index() {
        $data['user'] = $this->session->userdata['user'];
        if ($data['user'] == FALSE) {
            $t = new user();
            $t->DangNhap();
        } else {
            $this->load->view('sinhvien/sv-index', $data);
        }
    }

    public function page_lapdon() {
        $user = $this->session->userdata('user');
        $this->load->model('m_bieumau');
        $data['dsbieumau'] = $this->m_bieumau->getAll(1);
        $dsdonsv = $this->m_don_sv->getAllID($user['Ma_SV']);
        $data['dsdonsv'] = $dsdonsv;
        $this->load->view('sinhvien/page-lapdon', $data);
    }

    public function page_don_sv() {
        $this->checkUser();


        $dsdonsv = $this->m_don_sv->getAllID($this->user['Ma_SV']);
        $data['dsdonsv'] = $dsdonsv;
        $data['user'] = $this->session->userdata('user');

        $this->load->view('sinhvien/page-don-sv', $data);
    }

    public function xuly_them_don() {
        $this->checkUser();

        switch ($_POST['bt']) {

            case 0:
                $this->page_don_sv();
                break;
            case 1:
                $this->m_don_sv->them($this->user['Ma_SV'], common::ArrayToString($_POST['datainput']), $_POST['Ma_BieuMau']);
                $this->page_don_sv();
                break;

            default:

                break;
        }
    }

    public function xuly_dontrangthai() {
        $this->checkUser();



        switch ($this->input->get_post("bt")) {
            case 1 :


                $don = $this->m_don_sv->getOneID($this->input->get_post("Ma_Don_SV"));

                $data['datadon'] = $this->m_don_sv->getContentID($this->input->get_post("Ma_Don_SV"));
                $data['kieu'] = 1;
                $this->load->view('htmldoc/' . $don['Link_MauDon'], $data);

                break;
            case 2 :
                $this->m_don_sv->deleteOneID($this->input->get_post("Ma_Don_SV"));
                $this->page_lapdon();

                break;


            case 3 :
                $data = $this->m_don_sv->getContentID($this->input->get_post("Ma_Don_SV"));

                Excel::Output($data);
                print_r($data);
                $this->page_lapdon();

                break;
        }
    }

    public function page_tudanhgia() {
        $this->checkUser();
//        $date = date_create(date('d-m-Y'));
//        date_add($date, date_interval_create_from_date_string('30 days'));
//
//        $d = $this->m_diemrenluyen->thoidiemdanhgia();
//        echo strtotime($d['ThoiGian']);
//        echo '<br>';
//        echo strtotime(date('d-m-Y'));
//        echo '<br>';
//        echo strtotime(date_format($date, 'd-m-Y'));
//        exit();

        $data['user'] = $this->user;
        $data['dsdiem'] = $this->m_diemrenluyen->layketqua($this->session->userdata['user']['Ma_SV']);
        $data['HocKy'] = 6;
        $this->load->view("sinhvien/page-tudanhgia", $data);
    }

    public function xuly_tudanhgia() {
        $this->checkUser();
        $f = $this->m_diemrenluyen->them($this->user['Ma_SV'], $_POST);
        if ($f == FALSE) {
            echo 'Không thêm được';
        } else {
            $this->index();
        }
    }

    public function page_homthu() {
        $this->checkUser();

        $data['user'] = $this->session->userdata('user');
        $data['dshomthu'] = $this->m_homthu->getAll();


        $this->load->view("sinhvien/page-homthu", $data);
    }

    public function xuly_homthu() {
        $this->checkUser();



        $this->page_lapdon();
    }

    public function xuly_don_ds() {
        $this->checkUser();
        $data['user'] = $this->user;

        switch ($_POST['bt']) {
            case 0:
                $data = $this->m_don_sv->getOneID($_POST['Ma_Don_SV']);
                Excel::xuatfile2($data);

                //$this->m_don_sv->deleteOneID($_POST['Ma_Don_SV']);
                // $this->page_don_sv();
                break;
            case 1:


                break;
            case 2:
                
                $this->m_don_sv->deleteOneID($_POST['Ma_Don_SV']);
                
                $this->page_don_sv();

                break;
            case 3:
                $data = $this->m_don_sv->getOneID($_POST['Ma_Don_SV']);
               
                $this->m_don_sv->capnhat1($_POST['Ma_Don_SV'], 1);
                $noidung = 'Hệ thông đang chờ sinh viên ký tên và gửi đơn cho phong công tác sinh viên';
                $this->m_don_sv->capnhattrangthai($_POST['Ma_Don_SV'], $noidung, 1);
                Excel::xuatfile($data);

                break;
            case 4:


                $data = $this->m_don_sv->getOneID2($_POST['Ma_Don_SV']);
                //      common::showdata($data);
                $this->load->view('sinhvien/page-chitietdon', $data);
                break;


            default:
                break;
        }
    }

    public function downloadDon() {
        $this->checkUser();



        $this->load->model('m_bieumau');
        $don = $this->m_bieumau->getOneID($this->input->get_post('Ma_BieuMau'));
        common::dowloadFile(base_url() . 'public/doc', $don['TenFile']);
    }

    function form_bieumau() {

        $this->load->model('m_bieumau');
        $data['dsinput'] = $this->m_bieumau->getODuLieuId($_POST['Ma_BieuMau']);
        $data['Ma_BieuMau'] = $_POST['Ma_BieuMau'];
        $data['LoaiDon'] = $_POST['LoaiDon'];
        // echo '<pre>';
        // print_r($data);echo '</pre>';
        $this->load->view('sinhvien/page_nhapdulieu', $data);
    }

}

?>
